package ch.ethz.eai.webservice;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ch.ethz.eai.webservice.OurWebServiceStub.ResponseMessage;

public class ResultServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		ResponseMessage response;
		String action = request.getParameter("action");
		if(action.equals("setByAddress")){
			String address = request.getParameter("address");
			String feedback = request.getParameter("status");
			response = ServiceProxy.setWeatherStatusByAddress(address, feedback);
		}
		else if(action.equals("setByLocation")){
			String x = request.getParameter("x");
			String y = request.getParameter("y");
			String feedback = request.getParameter("status");
			response = ServiceProxy.setWeatherStatusByCoordinates(x, y, feedback);
			String msg;
			if(response != null){
				msg =  response.getMessage();
			}
			else msg = "Ooops, our service is too busy at the moment. Please try later.";
			try {
				JSONObject obj = new JSONObject();
				obj.put("msg", msg);

				resp.setContentType("application/json");
				resp.getWriter().println(obj.toString());
				return;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		else if(action.equals("getByAddress")){
			String address = request.getParameter("address");
			response = ServiceProxy.getProposalsByAddress(address);
		}
		else{
			String x = request.getParameter("x");
			String y = request.getParameter("y");
			response = ServiceProxy.getProposalsByGeoLocation(x, y);
		}
		
		if(response != null){
			System.out.println("Response valid");
			
			// status code
			int statusCode = response.getCode();

			if(statusCode == 1) {
				System.out.println("Success");
				// venues
				String[] venues =  response.getVenues();
				if(venues != null){
					for(String v : venues){
						System.out.println(v);
					}
				}
				String msg =  response.getMessage();
				System.out.println("Message" + msg);
				String weatherStatus = response.getWeatherStatus();
				//weather status is a string
				/*int status = Integer.parseInt(weatherStatus);
				switch(status){
					case -2: weatherStatus = "undefined"; break;
					case -1: weatherStatus = "unknown"; break;
					case 0 : weatherStatus = "clear"; break;
					case 1 : weatherStatus = "cloudy"; break;
					case 2 : weatherStatus = "rainy"; break;
					case 3 : weatherStatus = "stormy"; break;
					case 4 : weatherStatus = "snowy"; break;
				}*/
				System.out.println("Weather:  " + weatherStatus);
				
				try {
					JSONObject obj = new JSONObject();
					obj.put("weatherStatus", weatherStatus);
					obj.put("statusCode", statusCode);
					obj.put("msg", msg);
					if(venues != null){
						JSONArray POIs = new JSONArray(venues);
						obj.put("POIs", POIs);
					}
					else obj.put("POIs", "");

					resp.setContentType("application/json");
					resp.getWriter().println(obj.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			else if(statusCode == -1) {
				System.out.println("Failure");
				// message
				String msg = response.getMessage();
				System.out.println("Message:  " + msg);
				
				try {
					JSONObject obj = new JSONObject();
					obj.put("msg", msg);
					obj.put("statusCode", statusCode);

					resp.setContentType("application/json");
					resp.getWriter().println(obj.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			else if(statusCode == -2) {
				System.out.println("more actions");
				
				// addresses
				String[] addr =  response.getAddresses();
				if(addr != null){
					for(String a : addr){
						System.out.println(a);
					}
				}
				// message
				String msg = response.getMessage();
				
				try {
					JSONObject obj = new JSONObject();
					if(addr != null){
						JSONArray array = new JSONArray();
						for(String a : addr){
							
							array.put(a);
						}
						obj.put("addr", array);
					}
					else obj.put("addr", "");
					obj.put("msg", msg);
					obj.put("statusCode", statusCode);
					

					resp.setContentType("application/json");
					resp.getWriter().println(obj.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		else{
			System.out.println("Response null");
			String msg = "Ooops, our service is too busy at the moment. Please try later.";
			
			try {
				JSONObject obj = new JSONObject();
				obj.put("msg", msg);
				obj.put("statusCode", -1);

				resp.setContentType("application/json");
				resp.getWriter().println(obj.toString());
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}		
		
	}
}
